<?php
/**
 * 合同模块
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9 0009
 * Time: 下午 2:32
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\ContractVali;
use App\Model\Backstage\Contract;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;

class ContractController extends Controller
{
    protected  $model;
    public function __construct()
    {
        $this->model = new Contract();
    }




    /** 合同列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function lists(Request $request)
    {
        $map= array();
        if ($request->input('number')){
            $map['number'] = trim($request->input('number'));
        }
        if ($request->input('signatory')){
            $map['signatory'] = $request->input('signatory');
        }
        if ($request->input('customer_name')){   //客户姓名
            $map['customer_name'] = $request->input('customer_name');
        }
        if ($request->input('owner_name')){   //业主姓名
            $map['owner_name'] = $request->input('owner_name');
        }
        //dd($map);
        $res = $this->model->getList($map);
           if (sizeof($res) < 1){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }

    /** 合同添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $parameter = $request->except('token');
        if(!ContractVali::validate($parameter)){
            return $this->error(Error::PARAM_MISSING);
        }
       $res = $this->model->add($parameter);

        if (!$res){
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('添加成功');
    }

    /** 合同修改
     * @param Request $request
     */
    public function edit(Request $request)
    {
        $parameter = $request->except('token');
        if(!ContractVali::validate($parameter) || !isset($parameter['id'])){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->model->edit($parameter);
        if (!$res){
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('修改成功');
    }
    /** 删除合同
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');
        if(!isset($id) || $id == ''){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->model->deletes($id);
        if (!$res){
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('删除成功');
    }

}